from django.contrib.auth.models import AbstractUser
from django.db import models
from common.model_base import CoreModel, SoftDeleteModel, SoftDeleteManager

# 定义模型的一些常量
LEN_SHORT = 32
LEN_NORMAL = 64
LEN_MIDDLE = 128
LEN_LONG = 255
LEN_X_LONG = 1000
LEN_XX_LONG = 10000
LEN_XXX_LONG = 20000


class User(CoreModel, AbstractUser, SoftDeleteModel):
    """
    用户表
    """
    account = models.CharField(max_length=LEN_MIDDLE, unique=True, verbose_name="用户账号",
                               help_text="用户账号")
    email = models.EmailField(max_length=LEN_LONG, verbose_name="邮箱", null=True, blank=True, help_text="邮箱")
    mobile = models.CharField(max_length=LEN_LONG, verbose_name="电话", null=True, blank=True, help_text="电话")
    avatar = models.CharField(max_length=LEN_LONG, verbose_name="头像", null=True, blank=True, help_text="头像")
    name = models.CharField(max_length=40, verbose_name="姓名", help_text="姓名")
    GENDER_CHOICES = (
        (0, "未知"),
        (1, "男"),
        (2, "女"),
    )
    gender = models.IntegerField(
        choices=GENDER_CHOICES, default=0, verbose_name="性别", null=True, blank=True, help_text="性别"
    )
    role = models.ManyToManyField(to="Role", blank=True, verbose_name="关联角色", db_constraint=False,
                                  help_text="关联角色")
    dept = models.ForeignKey(
        to="Dept",
        verbose_name="所属部门",
        on_delete=models.PROTECT,
        db_constraint=False,
        null=True,
        blank=True,
        help_text="关联部门",
    )

    # 使用软删除 manager
    object = SoftDeleteManager()

    class Meta:
        verbose_name = "用户表"
        verbose_name_plural = verbose_name
        ordering = ("-create_datetime",)


class Dept(CoreModel):
    """
    部门表
    """
    name = models.CharField(max_length=LEN_NORMAL, verbose_name="部门名称", help_text="部门名称")
    owner = models.CharField(max_length=32, verbose_name="负责人", null=True, blank=True, help_text="负责人")
    status = models.BooleanField(default=True, verbose_name="部门状态", null=True, blank=True, help_text="部门状态")
    role = models.ManyToManyField(to="Role", blank=True, verbose_name="关联角色", db_constraint=False,
                                  help_text="关联角色")
    parent = models.ForeignKey(
        to="Dept",
        on_delete=models.CASCADE,
        default=None,
        verbose_name="上级部门",
        db_constraint=False,
        null=True,
        blank=True,
        help_text="上级部门",
    )

    class Meta:
        verbose_name = "部门表"
        verbose_name_plural = verbose_name


class Role(CoreModel):
    """
    角色表
    """
    name = models.CharField(max_length=LEN_NORMAL, verbose_name="角色名称", help_text="角色名称")
    key = models.CharField(max_length=LEN_NORMAL, unique=True, verbose_name="权限字符", help_text="权限字符")
    sort = models.IntegerField(default=1, verbose_name="角色顺序", help_text="角色顺序")
    status = models.BooleanField(default=True, verbose_name="角色状态", help_text="角色状态")
    admin = models.BooleanField(default=False, verbose_name="是否为admin", help_text="是否为admin")
    remark = models.TextField(verbose_name="备注", help_text="备注", null=True, blank=True)
    # menu = models.ManyToManyField(to="Menu", verbose_name="关联菜单", db_constraint=False, help_text="关联菜单")

    class Meta:
        verbose_name = "角色表"
        verbose_name_plural = verbose_name
        ordering = ("sort",)
